package com.example.document.local

import androidx.lifecycle.viewModelScope
import com.example.lib_base.BaseViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.launch

class LocalViewModel:BaseViewModel<LocalRepository,LocalState>() {
    override fun createState(): LocalState {
        return LocalState(false, mutableListOf())
    }

    override fun createResp(): LocalRepository {
        return LocalRepository()
    }

    fun scanFiles(){
        viewModelScope.launch {
            resp.scanFile()
                .catch {
                    it.printStackTrace()
                }
                .onStart {
                    mutableContainer.updateRequestState { copy(request = true,complete = false) }
                }.onCompletion {
                    mutableContainer.updateRequestState { copy(request = false,complete = true) }
                }
                .collect{
                    mutableContainer.updateUiState { copy(true,it) }
                }
        }
    }

}